package org.linlinjava.litemall.admin.web.distribution;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.admin.annotation.LoginAdmin;
import org.linlinjava.litemall.admin.web.UserController;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.DisMemberInfo;
import org.linlinjava.litemall.db.domain.LitemallUser;
import org.linlinjava.litemall.db.service.distribution.DisMemberInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 会员表控制器
 *
 * @author huangpu
 * @Date 2018-04-05 21:49:44
 */
@RestController
@RequestMapping("/admin/disMemberInfo")
public class DisMemberInfoController extends BaseController {
    private final Log logger = LogFactory.getLog(DisMemberInfoController.class);
    private String PREFIX = "/dist/disMemberInfo/";

    @Autowired
    DisMemberInfoService disMemberInfoService;

    @GetMapping("/list")
    public Object list(@LoginAdmin Integer adminId,
                       String username, String userId, String disModelId,
                       @RequestParam(value = "page", defaultValue = "1") Integer page,
                       @RequestParam(value = "limit", defaultValue = "10") Integer limit,
                       String sort) {
        if (adminId == null) {
            return ResponseUtil.fail401();
        }
        List<DisMemberInfo> memList = disMemberInfoService.querySelective(username, userId, disModelId, page, limit, sort);
        int total = disMemberInfoService.countSeletive(username, userId, disModelId, page, limit, sort);
        Map<String, Object> data = new HashMap<>();
        data.put("total", total);
        data.put("items", memList);
        return ResponseUtil.ok(data);
    }


    /**
     * 修改会员
     */
    @PutMapping(value = "/update")
    @ResponseBody
    public Object update(@RequestBody DisMemberInfo info) {
        logger.debug(info);
        try {
            disMemberInfoService.update(info);
        } catch (Exception e) {
            return ResponseUtil.fail(500, "更新失败，" + e.getMessage());
        }
        return ResponseUtil.ok(info);
    }

    @PostMapping("/create")
    public Object create(@RequestBody DisMemberInfo info){
        logger.debug(info);
        disMemberInfoService.add(info);
        return ResponseUtil.ok(info);
    }
}
